package org.gd.science.paybarier;

import org.gd.hellojaxb.utils.MarshallProperties;
import org.gd.hellojaxb.utils.Marshall;

import javax.xml.bind.JAXBException;
import java.io.*;
import java.net.URL;
import java.util.concurrent.atomic.AtomicInteger;

public class App {

    public static void main(String...args) throws JAXBException, IOException {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            URL url = App.class.getClassLoader().getResource("pay.barrier.config.xml");
            assert url != null;
            File file = new File(url.toURI());
            if(file.isFile()) {
                fileReader = new FileReader(file);
                bufferedReader = new BufferedReader(fileReader);
                AtomicInteger i = new AtomicInteger(0);
                System.out.println("=============================================");
                for(String line = bufferedReader.readLine();line!=null;line = bufferedReader.readLine()) {
                    System.out.printf("%3d: %s\n", i.incrementAndGet(), line);
                }
                System.out.println("=============================================");
            }
        } catch (Throwable e) {
            System.out.println(e);
        } finally {
            close(bufferedReader, fileReader);
        }

        BlockCardList blockCardList = new BlockCardList();
        blockCardList.add("2600*");
        blockCardList.add("2630*");

        MobilePayments mobilePayments = new MobilePayments()
                .setBlockCardList(blockCardList);

        ABankPayments aBankPayments = new ABankPayments()
                .setBlockCardList(blockCardList);

        Payments payments = new Payments()
                .setMobilePayments(mobilePayments)
                .setABankPayments(aBankPayments);

        String xml = Marshall.getInstance().marshal(
                payments,
                MarshallProperties.FORMATTED_OUTPUT,
                MarshallProperties.WITH_XML_HEADER
        );
        System.out.println(xml);
    }

    private static void close(Object...objects) {
        if(objects==null) {
            return;
        }
        for(Object o: objects) {
            if(o != null && o instanceof Closeable) {
                try {
                    ((Closeable)o).close();
                } catch (IOException e) {
                    System.out.println(e);
                }
            }
        }
    }
}
